//
//  TrendViewController.m
//  CaiPiao
//
//  Created by apple on 2018/5/18.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "TrendViewController.h"
#import <JavaScriptCore/JavaScriptCore.h>
#import <WebKit/WebKit.h>


@interface TrendViewController ()<UIGestureRecognizerDelegate,WKNavigationDelegate>

@property (weak, nonatomic) IBOutlet UIWebView *trendWebView;
@property (weak, nonatomic) IBOutlet UIView *buttonBoxView;


@property (weak, nonatomic) IBOutlet UIView *boardView;

@property (nonatomic,retain)WKWebView* TrendWebView_2;
@property (weak, nonatomic) IBOutlet UIView *trendWebBoxView;

@end

@implementation TrendViewController{
    NSString* currentURLString;
    BOOL firstLoad;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.title = @"走势图";
    
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:nil];
    longPress.delegate = self; //记得在.h文件里加上委托
    longPress.minimumPressDuration = 0.3; //这里为什么要设置0.4，因为只要大于0.5就无效，我像大概是因为默认的跳出放大镜的手势的长按时间是0.5秒，
    //如果我们自定义的手势大于或小于0.5秒的话就来不及替换他的默认手势了，这是只是我的猜测。但是最好大于0.2秒，因为有的pdf有一些书签跳转功能，这个值太小的话可能会使这些功能失效。
    
    [self.trendWebView addGestureRecognizer:longPress];
    firstLoad = YES;
//    [EasyLodingView showLodingText:@"正在努力加载中..."];
//    [self loadLottoryTrendWith:@"http://news.cp.lovedou.com/lotteryChart?lottery=7&type=trend"];
    
    [self checkSelectedLottery];
    

}

-(void)loadLottoryTrendWith:(NSString*)urlString{
    self.boardView.hidden = NO;
    self.trendWebView.hidden = YES;
//    [self.trendWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
    
    self.TrendWebView_2.hidden = YES;
    self.TrendWebView_2 = [[WKWebView alloc] initWithFrame:CGRectMake(0, 10, self.trendWebBoxView.frame.size.width, self.trendWebBoxView.frame.size.height)];
    [self.trendWebBoxView insertSubview:self.TrendWebView_2 belowSubview:self.boardView];
    self.TrendWebView_2.navigationDelegate = self;
    [self.TrendWebView_2 loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
}
//@"9":@"ks",
//@"5":@"福彩3D",
//@"6":@"排列三",
//@"7":@"11x5",
//@"3":@"双色球",
//@"4":@"大乐透"

-(void)checkSelectedLottery{
//    [LotteryManager sharedManager].currentLotteryID;
    NSInteger seletedType = 0;
    switch ([[LotteryManager sharedManager].currentLotteryID integerValue]) {
        case 3:
            seletedType = 101;
            break;
        case 4:
            seletedType = 103;
            break;
        case 5:
            seletedType = 102;
            break;
        case 6:
            seletedType = 106;
            break;
        case 7:
            seletedType = 107;
            break;
        case 9:
            seletedType = 104;
            break;
        default:
            break;
    }
    
    for (UIView* view in self.buttonBoxView.subviews) {
        if (view.tag>100) {
            UIButton* styleButton = (UIButton*)view;
            [styleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
    }
    UIButton* sender = [self.buttonBoxView viewWithTag:seletedType];
    [sender setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    
    [self loadURLWithTag:seletedType];
    
}

-(void)loadURLWithTag:(NSInteger)tag{
    self.boardView.hidden = NO;
    NSString* urlString = @"";
    switch (tag) {
        case 101:
            //            双色球
            firstLoad = YES;
            urlString = @"https://caipiao.163.com/t/trend/ssq/";
            break;
        case 102:
            //    福彩
            firstLoad = YES;
            urlString = @"https://caipiao.163.com/t/trend/x3d/";
            break;
        case 103:
            //    大乐透
            firstLoad = YES;
            urlString = @"https://caipiao.163.com/t/trend/dlt/";
            break;
        case 104:
            //    快三
            firstLoad = YES;
            urlString = @"https://caipiao.163.com/t/trend/jskuai3/";
            break;
        case 105:
            //    江苏11选5
            urlString = @"https://news.cp.lovedou.com/lotteryChart?lottery=14&type=trend";
            break;
        case 106:
            //    排列三
            firstLoad = YES;
            urlString = @"https://caipiao.163.com/t/trend/pl3/";
            break;
        case 107:
            //    11X5
            urlString = @"https://news.cp.lovedou.com/lotteryChart?lottery=7&type=trend";
            break;
        case 108:
            //    夺金11选5
            urlString = @"https://news.cp.lovedou.com/lotteryChart?lottery=8&type=trend";
            break;
        default:
            break;
    }
    currentURLString = urlString;
    [EasyLodingView showLodingText:@"正在努力加载中..."];
    [self loadLottoryTrendWith:urlString];
}

- (IBAction)selectedStyleAction:(UIButton *)sender {
    for (UIView* view in self.buttonBoxView.subviews) {
        if (view.tag>100) {
            UIButton* styleButton = (UIButton*)view;
            [styleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
    }
    
    [sender setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    
    [self loadURLWithTag:sender.tag];
}




#pragma mark - GestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return NO; //这里一定要return NO,至于为什么大家去看看这个方法的文档吧。
    //还有就是这个委托在你长按的时候会被多次调用，大家可以用nslog输出gestureRecognizer和otherGestureRecognizer
    //看看都是些什么东西。
}



#pragma mark - UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    if ([request.URL.absoluteString hasPrefix:@"https://www.qipaigame1.com/"]) {
        return NO;
    }
    return YES;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{


    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.trendWebView.hidden = NO;
    });
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('esDialog0').style.display = 'NONE'"];
        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('pageHead').style.display = 'NONE'"];
        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('m-carousel-wrap topHiPos2')[0].style.display = 'NONE'"];
        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('m-banner')[0].style.display = 'NONE'"];
        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('m-bottomRight')[0].style.display = 'NONE'"];
        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('footer-lottery')[0].style.display = 'NONE'"];
        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('footer-source')[0].style.display = 'NONE'"];
        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('footer-copy')[0].style.display = 'NONE'"];
        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('es-dialog-wrap')[0].style.display = 'NONE'"];
        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('es-dialog-body m-dialog-body')[0].style.display = 'NONE'"];
        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('m-freeBonus')[0].style.display = 'NONE'"];
    });
    
    if (firstLoad) {
        self.boardView.hidden = NO;
        @try{
            [self loadLottoryTrendWith:currentURLString];
        }@catch(NSException* e){
            NSLog(@"%@",e);
        //除非try里面执行代码发生了异常，否则这里的代码不会执行
        }@finally{
        
        //不管什么情况都会执行，包括try catch 里面用了return ,可以理解为只要执行了try或者catch，就一定会执行 finally
        }
        
        firstLoad = NO;
        return;
    }
    
    [EasyLodingView hidenLoding];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.boardView.hidden = YES;
    });

}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
   
}

#pragma mark - WKNavigationDelegate
- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.TrendWebView_2.hidden = NO;
    });
    
    
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [webView evaluateJavaScript:@"document.getElementById('esDialog0').style.display = 'NONE'" completionHandler:^(id _Nullable response, NSError * _Nullable error) {
            
        }];
        [webView evaluateJavaScript:@"document.getElementById('pageHead').style.display = 'NONE'" completionHandler:^(id _Nullable response, NSError * _Nullable error) {
            
        }];
        [webView evaluateJavaScript:@"document.getElementsByClassName('m-carousel-wrap topHiPos2')[0].style.display = 'NONE'" completionHandler:^(id _Nullable response, NSError * _Nullable error) {
            
        }];
        [webView evaluateJavaScript:@"document.getElementsByClassName('m-banner')[0].style.display = 'NONE'" completionHandler:^(id _Nullable response, NSError * _Nullable error) {
            
        }];
        [webView evaluateJavaScript:@"document.getElementsByClassName('m-bottomRight')[0].style.display = 'NONE'" completionHandler:^(id _Nullable response, NSError * _Nullable error) {
            
        }];
        [webView evaluateJavaScript:@"document.getElementsByClassName('footer-lottery')[0].style.display = 'NONE'" completionHandler:^(id _Nullable response, NSError * _Nullable error) {
            
        }];
        [webView evaluateJavaScript:@"document.getElementsByClassName('footer-source')[0].style.display = 'NONE'" completionHandler:^(id _Nullable response, NSError * _Nullable error) {
            
        }];
        [webView evaluateJavaScript:@"document.getElementsByClassName('footer-copy')[0].style.display = 'NONE'" completionHandler:^(id _Nullable response, NSError * _Nullable error) {
            
        }];
        [webView evaluateJavaScript:@"document.getElementsByClassName('es-dialog-wrap')[0].style.display = 'NONE'" completionHandler:^(id _Nullable response, NSError * _Nullable error) {
            
        }];
        [webView evaluateJavaScript:@"document.getElementsByClassName('es-dialog-body m-dialog-body')[0].style.display = 'NONE'" completionHandler:^(id _Nullable response, NSError * _Nullable error) {
            
        }];
        [webView evaluateJavaScript:@"document.getElementsByClassName('m-freeBonus')[0].style.display = 'NONE'" completionHandler:^(id _Nullable response, NSError * _Nullable error) {
            
        }];
    });
    
    
    if (firstLoad) {
        self.boardView.hidden = NO;
        @try{
            [self loadLottoryTrendWith:currentURLString];
        }@catch(NSException* e){
            NSLog(@"%@",e);
            //除非try里面执行代码发生了异常，否则这里的代码不会执行
        }@finally{
            
            //不管什么情况都会执行，包括try catch 里面用了return ,可以理解为只要执行了try或者catch，就一定会执行 finally
        }
        
        firstLoad = NO;
        return;
    }
    
    [EasyLodingView hidenLoding];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.boardView.hidden = YES;
    });
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
